package org.an.demo.view;

/**
 * 创建时间：2017年02月15日11:39 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import org.an.demo.R;

public class CheckView extends View {

  private static final int ANIM_END = 0;         //动画状态-结束
  private static final int ANIM_CHECK = 1;        //动画状态-开启
  private static final int ANIM_UNCHECK = 2;      //动画状态-关闭

  private int mWidth, mHeight;        // 宽高
  private Handler mHandler;           // handler

  private Paint mPaint;
  private Bitmap okBitmap;
  private Rect mRect;

  private int animCurrentPage;       // 当前页码
  private int animMaxPage = 20;           // 总页数
  private int animDuration = 500;         // 动画时长
  private int animState = ANIM_END;      // 动画状态

  public CheckView(Context context) {
    super(context, null);
  }

  public CheckView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  /**
   * 初始化
   */
  private void init() {

    mPaint = new Paint();
    mPaint.setColor(0xffFF5317);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);

    okBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.correct);
    mRect =
        new Rect(0, 0, okBitmap.getWidth() * animCurrentPage / animMaxPage, okBitmap.getHeight());
    mHandler = new Handler() {
      @Override public void handleMessage(Message msg) {
        if (animState == ANIM_END) return;

        if (animState == ANIM_CHECK) {
          if (animCurrentPage < animMaxPage) {
            animCurrentPage++;
            invalidate();
            update();
          } else {
            animState = ANIM_END;
          }
        } else {
          if (animCurrentPage > 0) {
            animCurrentPage--;
            invalidate();
            update();
          } else {
            animState = ANIM_END;
          }
        }
      }
    };
  }

  /**
   * View大小确定
   */
  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mWidth = w;
    mHeight = h;
  }

  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, mPaint);
    //canvas.save();
    // 移到Bitmap居中的左上角
    canvas.translate(mWidth / 2 - okBitmap.getWidth() / 2, mHeight / 2 - okBitmap.getHeight() / 2);
    // 得到图像选区
    mRect.right = okBitmap.getWidth() * animCurrentPage / animMaxPage;
    // 第二个参数srcRect是图像选区（会进行裁剪），第三个参数dstRect是绘制区域，一定会沾满改区域（拉伸）
    // src要与dst保持一致，否则图像就会被拉伸
    canvas.drawBitmap(okBitmap, mRect, mRect, null);
    //canvas.restore();
  }

  /**
   * 选择
   */
  public void check() {
    if (animState != ANIM_END || animCurrentPage == animMaxPage) return;
    animState = ANIM_CHECK;
    update();
  }

  /**
   * 取消选择
   */
  public void unCheck() {
    if (animState != ANIM_END || animCurrentPage == 0) return;
    animState = ANIM_UNCHECK;
    update();
  }

  public void end() {
    if (animState == ANIM_END) return;
    animCurrentPage = animMaxPage;
    update();
  }

  private void update() {
    mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);
  }

  /**
   * 设置动画时长
   */
  public void setAnimDuration(int animDuration) {
    if (animDuration <= 0) return;
    this.animDuration = animDuration;
  }

  /**
   * 设置背景圆形颜色
   */
  public void setBackgroundColor(int color) {
    mPaint.setColor(color);
  }
}
